package practice.thread.talk.server;

import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;

public class AppContext {
	private static final AppContext instance = new AppContext();

	public static AppContext getInstance() {
		return instance;
	}


	private List<ClientThread> clients = new CopyOnWriteArrayList<ClientThread>();
	private Queue<String> messages = new ConcurrentLinkedQueue<String>();

	private AppContext() {

	}


	public synchronized int getConnectNumber() {
		return getCloneClients().size();
	}

	public List<ClientThread> getCloneClients() {
		return Collections.unmodifiableList(clients);
	}

	public void addClient(ClientThread client) {
		synchronized (clients) {
			clients.add(client);
		}
	}

	public void removeClient(ClientThread client) {
		synchronized (clients) {
			clients.remove(client);
		}

	}

//	public Queue<String> getMessages() {
//		return messages;
//	}
	
	public void pushMessage(String messgae){
		messages.offer(messgae);
	}
	
	public String popMessage(){
		return messages.poll();
	}

}
